﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;
 

public partial class Controls_HomePage_ctrNews_Homepage : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage() -1;
    private int nDefaultLang = Support.GetDefaultLanguage() ;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindData();
        }
    }

    private void BindData()
    {
        lblNews.Text = MultipleLanguage.NEWS2[nCurrentLang - 1];
     
        int nPageCount = 0;
        News objNews = new News();
        DataTable dtbNews = new DataTable();
        dtbNews = objNews.Search(3, 0, "", 68, nDefaultLang, nCurrentLang, EnumeType.ACTIVE, 1, 1000, ref nPageCount);  
        rptNews.DataSource = dtbNews;
        rptNews.DataBind();
    }
    
    protected void rptNews_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL  + "?" + Config.NEWS_ID + "=" + nID;
                Response.Redirect(sDetail);
                break;

        }
    }
    protected void rptNews_ItemDataBound1(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            HyperLink lnkTitle = (HyperLink)e.Item.FindControl("lnkTitle");
            Image imgnews = (Image)e.Item.FindControl("imgnews");
            Label lblInitContent = (Label)e.Item.FindControl("lblInitContent");
            HyperLink lnkimgNews = (HyperLink)e.Item.FindControl("lnkimgNews");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));
            string sTitle = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle"));
            lnkimgNews.NavigateUrl = Config.ROOT + Pages.FrontEnds.PROJECT_DETAILS + "?" + Config.PAGE_ID + "=" + nID;
            lnkTitle.NavigateUrl = Config.ROOT + Pages.FrontEnds.PROJECT_DETAILS + "?" + Config.PAGE_ID + "=" + nID;
            string sInit = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "InitContent"));
            if (sTitle.Length > 0)
            {
                lnkTitle.Text = sTitle;
            }
            else
            {
                lnkTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle_Default"));
            }

            if (sInit.Length == 0)
            {
                sInit = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "InitContent_Default"));
            }

            if (sInit.Length > 130)
            {
                sInit = ConvertData.TruncateString(sInit, 120) + Config.DOT;
            }

            lblInitContent.Text = sInit;

            imgnews.ImageUrl = Config.ROOT + Config.IMAGE_NEWS_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Image"));
            //btnImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));
        }
    }
}